package com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.injector;

import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.MybatisPlusVersion;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.config.GlobalConfig;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.incrementer.DefaultIdentifierGenerator;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.incrementer.IdentifierGenerator;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.toolkit.GlobalConfigUtils;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.toolkit.IdWorker;
import com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.toolkit.SqlHelper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * <p>
 * InjectorBuilder
 * </p>
 *
 * @author zxm
 * @date 2021/08/16 11:11
 * @since 2021/08/16 11:11
 */
public class InjectorBuilder  extends SqlSessionFactoryBuilder {
    /**
     * mybatis plus SQL 自动注入器
     * @param mapperClass
     * @param configuration
     */
    public static void sqlInjector(Class<?> mapperClass, Configuration configuration) {

        //mybatis plus SQL 自动注入器
        // TODO 注入 CURD 动态 SQL , 放在在最后, because 可能会有人会用注解重写sql
        //扫描mapper 时
        if (GlobalConfigUtils.isSupperMapperChildren(configuration, mapperClass)) {
            String resource = mapperClass.getName().replace(".", "/") + ".java (best guess)";
            MapperBuilderAssistant assistant = new MapperBuilderAssistant(configuration, resource);
            assistant.setCurrentNamespace(mapperClass.getName());
            //
            if(isSqlInjector(mapperClass)){

                //注入sql
                GlobalConfigUtils.getSqlInjector(configuration).inspectInject(assistant, mapperClass);
            }else {
                //只注入表数据
                TableInfoInjector.inspectInject(assistant, mapperClass);
            }

        }

    }

    /**
     * 暂时没啥用
     * @param configuration
     */
    public static void buildSqlSessionFactory(Configuration configuration) {
        GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
        final SqlSessionFactory sqlSessionFactory =  new InjectorBuilder().build( configuration);
        SqlHelper.FACTORY = sqlSessionFactory;

        // TODO 打印骚东西 Banner
        if (globalConfig.isBanner()) {
            System.out.println(" _ _   |_  _ _|_. ___ _ |    _ ");
            System.out.println("| | |\\/|_)(_| | |_\\  |_)||_|_\\ ");
            System.out.println("     /               |         ");
            System.out.println("                        " + MybatisPlusVersion.getVersion() + " ");
        }
    }

    /**
     * 暂时没啥用
     * @param configuration
     */
    public SqlSessionFactory build(Configuration config) {
        Configuration configuration =  config;
        GlobalConfig globalConfig = GlobalConfigUtils.getGlobalConfig(configuration);
        final IdentifierGenerator identifierGenerator;
        if (globalConfig.getIdentifierGenerator() == null) {
            if (null != globalConfig.getWorkerId() && null != globalConfig.getDatacenterId()) {
                identifierGenerator = new DefaultIdentifierGenerator(globalConfig.getWorkerId(), globalConfig.getDatacenterId());
            } else {
                identifierGenerator = new DefaultIdentifierGenerator();
            }
            globalConfig.setIdentifierGenerator(identifierGenerator);
        } else {
            identifierGenerator = globalConfig.getIdentifierGenerator();
        }
        //TODO 这里只是为了兼容下,并没多大重要,方法标记过时了.
        IdWorker.setIdentifierGenerator(identifierGenerator);

        if (globalConfig.isEnableSqlRunner()) {
            new SqlRunnerInjector().inject(configuration);
        }

        SqlSessionFactory sqlSessionFactory = super.build(configuration);

        // 缓存 sqlSessionFactory
        globalConfig.setSqlSessionFactory(sqlSessionFactory);

        return sqlSessionFactory;
    }

    /**
     * 是否注入sql
     * @param mapperClass
     * @return
     */
    public static boolean isSqlInjector(Class<?> mapperClass) {
        Class<?> superMapperClass = com.maas.util.mybatis.jpa.littlenb.conditions.plus.core.mapper.BaseMapper.class;
        return superMapperClass.isAssignableFrom(mapperClass);
    }


}
